package pro.lepers.portfolio.core {
	import caurina.transitions.Equations;
	import caurina.transitions.Tweener;

	import pro.lepers.portfolio.Consts;
	import pro.lepers.portfolio.event.MasterModelEvent;

	import flash.events.EventDispatcher;

	/**
	 * 
	 * @see MasterModelEvent#SCALE_CHANGE
	 */[Event( name = "mm_SCALE_CHANGE", type = "MasterModelEvent" )]

	/**
	 * @author plepers
	 */
	internal class MasterModel extends EventDispatcher {
		
		
		
		public function MasterModel(  ) {
			super( );
			if( instance != null ) throw new Error( "pro.lepers.portfolio.core.MasterModel est deja instancié" );
		}


		//_____________________________________________________________________________
		//																   GLOBAL SCALE

		public function downScale () : void {
			if ( _downScaled ) return;
			_downScaled = true;
			if( ! _scaleChanging )
				dispatchEvent( new MasterModelEvent( MasterModelEvent.SCALE_START ) );
			_scaleChanging = true;
			dispatchEvent( new MasterModelEvent( MasterModelEvent.DOWN_SCALE ) );
			Tweener.addTween( this, { 
										globalScale : Consts.SCALE_FX, 
										time : 1, 
										skipUpdates : 1,
										onComplete : dsComplete,
										transition : Equations.easeInOutQuad
										} );
		}


		public function upScale () : void {
			if ( ! _downScaled ) return;
			_downScaled = false;
			if( ! _scaleChanging )
				dispatchEvent( new MasterModelEvent( MasterModelEvent.SCALE_START ) );
			_scaleChanging = true;
			dispatchEvent( new MasterModelEvent( MasterModelEvent.UP_SCALE ) );
			Tweener.addTween( this, { 	
										globalScale : 1, 
										time : .2,
										skipUpdates : 1,
										onComplete : usComplete ,
										transition : "linear"
									} );
		}
		
		
		public function get downScaled () : Boolean {
			return _downScaled;
		}
		
		public function get globalScale ( ) : Number {
			return _globalScale;
		}

		private function dsComplete () : void {
			_scaleChanging = false;
			dispatchEvent( new MasterModelEvent( MasterModelEvent.SCALE_STOP ) );
			dispatchEvent( new MasterModelEvent( MasterModelEvent.DOWN_SCALED ) );
		}

		private function usComplete () : void {
			_scaleChanging = false;
			dispatchEvent( new MasterModelEvent( MasterModelEvent.SCALE_STOP ) );
			dispatchEvent( new MasterModelEvent( MasterModelEvent.UP_SCALED ) );
		}
		
		public function set globalScale ( val :  Number ) : void {
			if( _globalScale ==val) return;
			_globalScale = val;
			_scaleProgress = (_globalScale - Consts.SCALE_FX) / ( 1 - Consts.SCALE_FX );
			dispatchEvent( new MasterModelEvent( MasterModelEvent.SCALE_CHANGE ) );
		}

		public function get scaleProgress () : Number {
			return _scaleProgress;
		}

		
		
		
		private var _scaleProgress : Number = 1;
		private var _globalScale : Number = 1;
		private var _scaleChanging : Boolean = false;
		
		private var _downScaled : Boolean = false;

		//_____________________________________________________________________________
		//																	  SINGLETON
		private static var instance : MasterModel;
		
		
		internal static function start( ) : MasterModel {
			if (instance == null)
				instance = new MasterModel();
			return instance;
		}
		
	}
}
